package cn.net.cfss.fgbp.provider.manage.service;

import cn.net.cfss.fgbp.provider.manage.biz.FscTenantBiz;
import cn.net.cfss.fgbp.provider.manage.utils.FormatUtils;
import cn.net.cfss.fgbp.provider.manage.utils.ResultUtil;
import cn.net.cfss.fgbp.spi.manage.constant.DeleteFlagEnum;
import cn.net.cfss.fgbp.spi.manage.dto.FscTenantDto;
import cn.net.cfss.fgbp.spi.manage.vo.FscTenantVo;
import cn.net.cfss.fgbp.spi.manage.vo.ResultDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2020-2050</p>
 * <p>Company: cfss</p>
 *
 * @author lyg
 * @version 0.0.1
 * @date 2020/12/11 11:52
 * @other 以下修改下及其它信息
 * <p>v0.0.2,lyg,这是示例</p>
 */
@Slf4j
@Service
public class MyFscTenantService extends MyBaseService {

    @Autowired
    FscTenantBiz fscTenantBiz;

    @Autowired
    private CacheService cacheService;


    public ResultDto<FscTenantVo> add(FscTenantVo fscTenantVo) {
        String tenantName = fscTenantVo.getTenantName();
        //租户名称唯一不可重复
        FscTenantDto search = new FscTenantDto();
        search.setDeleteFlag(DeleteFlagEnum.NOT_DEL);
        search.setTenantName(tenantName);
        Long count = fscTenantBiz.selectCount(search);
        if (count > 0) {
            return ResultUtil.returnFail("租户名称重复");
        }
        Boolean legalWorkTime = FormatUtils.checkFormatTenantWorkTime(fscTenantVo.getWorkTime());
        if (!legalWorkTime) {
            return ResultUtil.returnFail("工作时间段格式不正确");
        }

        String code = fscTenantBiz.queryNewestCode();
        fscTenantVo.setId(fscTenantBiz.getUID());
        fscTenantVo.setTenantCode(getNewestCode(code,"00001","TT"));
        fscTenantVo.setCreateTime(new Date());
        fscTenantVo.setUpdateTime(new Date());
        fscTenantVo.setDeleteFlag(DeleteFlagEnum.NOT_DEL);
        fscTenantBiz.insert(fscTenantVo);


        //新增完租户信息后需要设置缓存
        cacheService.updateTenant(fscTenantVo);

        return new ResultDto<>(true, fscTenantVo, null);
    }

    public ResultDto<FscTenantVo> update(FscTenantVo fscTenantVo) {
        FscTenantDto fscTenantDto = fscTenantBiz.selectById(fscTenantVo.getId());
        if (fscTenantDto == null) {
            return new ResultDto<>(false, null, "租户查询为空");
        }
        if ((fscTenantVo.getTenantName() != null) && (!fscTenantDto.getTenantName().equals(fscTenantVo.getTenantName()))) {
            //如果名称不一致，需要判断修改后的名称是否会出现重复
            FscTenantDto search = new FscTenantDto();
            search.setDeleteFlag(DeleteFlagEnum.NOT_DEL);
            search.setTenantName(fscTenantVo.getTenantName());
            Long count = fscTenantBiz.selectCount(search);
            if (count > 0) {
                return new ResultDto<>(false, null, "租户名称重复");
            }
        }
        Boolean legalWorkTime = FormatUtils.checkFormatTenantWorkTime(fscTenantVo.getWorkTime());
        if (!legalWorkTime) {
            return ResultUtil.returnFail("工作时间段格式不正确");
        }

        fscTenantVo.setUpdateTime(new Date());
        fscTenantBiz.updateSelectiveById(fscTenantVo);

        //更新DB完成后，更新缓存
        cacheService.updateTenant(fscTenantBiz.selectById(fscTenantVo.getId()));

        return new ResultDto<>(true, fscTenantVo, null);
    }
}
